package main.Communication;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.net.SocketException;

import main.Communication.Interface.recvInterface;

/**
 * 客户端线程接收数据
 * @author svitter
 */
class recvThread implements Runnable, recvInterface {
    private ObjectInputStream 	dis   		=   null;
    private boolean				bConnected;
    private Object				mes;
    
	public recvThread() {

	}
    public recvThread(ObjectInputStream dis) {
    	this.dis = dis;
    }
    
	/**
	 * 转换为连接状态
	 */
	public void setConnect(boolean bConnected) {
		this.bConnected = bConnected;
	}

	public boolean getConnect() {
		return this.bConnected;
	}

	/**
     * 持续接受服务端信息
     */
	public void run() {
		try {
			while (bConnected) {
				mes = dis.readObject(); // 拿到数据
				handle(mes);
			}
		} catch (SocketException e) {
			e.printStackTrace();
            System.out.println("[Client]: Server stop service.");
		} catch (IOException e) {
			e.printStackTrace();
            System.out.println("[Client]: 无法获取数据");
            System.out.println("[Client]: Server stop service.");
		} catch (ClassNotFoundException e2) {
			e2.printStackTrace();
            System.out.println("[Client]: 找不到类");
		}
	}

	/**
	 * 处理数据
	 * @param umd
	 */
	public void handle(Object umd) {
		// init
        System.out.println("[Client]: run handle().");
	}

	/**
	 * 设置ObjectDis值
	 * @param dis
	 */
	public void setDis(ObjectInputStream dis) {
		this.dis = dis;
	}
}